package examples;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import utils.XmlManager;

public class SoapExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/*
		SAXBuilder builder = new SAXBuilder();
		try {
			Element e = builder.build(new URL("http://localhost:6789/xml/location.xml")).getRootElement();
			
			System.out.println();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
		
		 try {
			 
//			 String containerID = "0";
//			 String SearchCriteria = "0";
//			 String Filter = "0";
//			 String StartingIndex = "0";
//			 String RequestedCount = "0";
//			 String SortCriteria = "0";
			 
			 //TODO TESTANDO MANUALMENTE WINDOWS MEDIA PLAYER
			 
		      String xmldata =  "<?xml version=\"1.0\"?>" +
		      "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" \r\n" +
		      			"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" +
		      		"\t<s:Body>\r\n" +
		      		
//		      			"\t\t<u:Search xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">\r\n" +
//		      				"\t\t\t<ContainerID>0</ContainerID>\r\n" +
//		      				"\t\t\t<SearchCriteria>*</SearchCriteria>\r\n" +
//		      				"\t\t\t<Filter>*</Filter>\r\n" +
//		      				"\t\t\t<StartingIndex>0</StartingIndex>\r\n" +
//		      				"\t\t\t<RequestedCount>4</RequestedCount>\r\n" +
//		      				"\t\t\t<SortCriteria></SortCriteria>\r\n" +
//		      			"\t\t</u:Search>\r\n" +		
		      		
		      			"\t\t<Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">\r\n" +
		      				"\t\t\t<BrowseFlag>BrowseDirectChildren</BrowseFlag>\r\n" +
	      					"\t\t\t<ObjectID>0</ObjectID>\r\n" +
	      					//"\t\t\t<SearchCriteria>*</SearchCriteria>\r\n" +
	      					"\t\t\t<Filter>*</Filter>\r\n" +
	      					"\t\t\t<StartingIndex>0</StartingIndex>\r\n" +
	      					"\t\t\t<RequestedCount>3</RequestedCount>\r\n" +
	      					"\t\t\t<SortCriteria></SortCriteria>\r\n" +
	      				"\t\t</Browse>\r\n" +
	      			
		      		"\t</s:Body>\r\n" +
		      "</s:Envelope>\r\n";
		      
		      System.out.println("XML de enviado:\n");
		      System.out.println(xmldata + "\n");
					
		      //Create socket
		      String hostname = "192.168.33.1";
		      int port = 2869;
		      InetAddress  addr = InetAddress.getByName(hostname);
		      Socket sock = new Socket(addr, port);
		      
		      //String soapAction = "\"urn:schemas-upnp-org:service:ContentDirectory:1 #Search\"";
		      String soapAction = "\"urn:schemas-upnp-org:service:ContentDirectory:1 #Browse\"";
					
		      //Send header
		      String path = "/upnphost/udhisapi.dll?control=uuid:e99a5378-d936-443c-b087-e314222d606f+urn:upnp-org:serviceId:ContentDirectory";
		      BufferedWriter  wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
		      // You can use "UTF8" for compatibility with the Microsoft virtual machine.
		      wr.write("POST " + path + " HTTP/1.0\r\n");
		      wr.write("Host: "+hostname+":"+port+"\r\n");
		      wr.write("Content-Length: " + xmldata.length() + "\r\n");
		      wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
		      wr.write("SOAPACTION: " + soapAction + "\r\n");
		      wr.write("\r\n");
					
		      //Send data
		      wr.write(xmldata);
		      wr.flush();
					
		      // Response
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					sock.getInputStream()));
			String line;
			
			System.out.println("RESPOSTA: \n");
			while ((line = rd.readLine()) != null)
				System.out.println(line);
		} catch (Exception e) {
			e.printStackTrace();
		}
		  
		System.out.println("\n"); 
		
	}

}
